package com.hongxin.nw.netty.mq_info;

import com.alibaba.fastjson.JSONObject;
import com.hongxin.nw.netty.model.Transfer;
import com.hongxin.nw.netty.model.analysis.info.Mrena1_13;
import com.hongxin.nw.netty.mq_info.domain.CMD108;
import com.hongxin.nw.netty.utils.BinaryTools;
import com.hongxin.nw.netty.utils.DateUtil;
import com.hongxin.nw.netty.utils.JSONUtils;
import com.hongxin.nw.netty.utils.ModifyJsonKeyUtil;
import com.hongxin.nw.netty.values.Catalogue;
import com.hongxin.nw.netty.values.StaticValue;
import com.hongxin.nw.netty.values.TypeIdentif;

import java.util.Date;

import static com.hongxin.nw.netty.values.StaticValue.*;

/**
 * @remark 开始充电108
 * @author: GuoXiaolin
 * @date 2019/6/21 19:29
 * @Version 1.0
 * @Descibe
 */
public class StartCharge implements  IMq {
    @Override
    public JSONObject sendMqInfo(Transfer transfer)  throws Exception{
        Mrena1_13 dataItem = (Mrena1_13) transfer.getObj();
        if(StaticValue.START_FAIL.equals(dataItem.getExcRes())){
            return JSONUtils.getRoot(-1, VAL_23, StaticValue.VAL_NULL);
        }
        if(StaticValue.START_CONFIRM.equals(dataItem.getExcRes())){
            return JSONUtils.getRoot(-1,VAL_33,StaticValue.VAL_NULL);
        }
        CMD108 cmd108 = new CMD108();
        cmd108.setMachineId(dataItem.getCo());
        cmd108.setCpOrderId(dataItem.getTranNum());
        cmd108.setInterfaceNumber(String.valueOf(Integer.parseInt(dataItem.getChargeIdentify())+1));
        cmd108.setStartMode(dataItem.getChargeStartType());
        cmd108.setUserId(dataItem.getPhysicalCard());
        if (BinaryTools.isFF(dataItem.getPhysicalCard(), TypeIdentif.BYTE_PHYSICS_NUM.getByteLength())){
            cmd108.setUserId("0");
        }
        cmd108.setStartDateTime(DateUtil.getTimeFormat(new Date(),"yyyy-MM-dd HH:mm:ss.SSS"));
        cmd108.setStartMode((Integer.parseInt(dataItem.getChargeStartType())) == 1 ? "0":"1");

        JSONObject json = (JSONObject) JSONObject.toJSON(cmd108);
        json.put(Catalogue.HXCMD,Catalogue.CMD108);
        JSONObject mqJson = ModifyJsonKeyUtil.removeEmptyKey(json);



        return JSONUtils.getRoot(0, VAL_24, mqJson);
    }
}
